//
//  HomePageViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/9/15.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "HomePageViewController.h"
#import "BLHeader.h"
#import "HomePageTableViewCell.h"
#import <MJRefresh.h>
#import "HomeNet.h"

@interface HomePageViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *images;
@property (nonatomic, assign) BOOL hasMore;
@property (nonatomic, assign) NSInteger index;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)getProductList{
    if ([self.tableView.mj_header isRefreshing]) {
        self.index = 0;
    }
    [HomeNet getImageList:[NSString stringWithFormat:@"%li", self.index] compelter:^(id result, NSError *error) {
        
        if ([self.tableView.mj_header isRefreshing]) {
            self.images = result;
        }
        self.hasMore = [(NSArray *)result count] == 15 ? YES : NO;
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = NO;
        self.tableView.mj_footer.state = MJRefreshStateIdle;
        [self.tableView reloadData];
    }];
}

- (void)getMore{
    self.index = _index + 15;
    [HomeNet getImageList:[NSString stringWithFormat:@"%li", self.index] compelter:^(id result, NSError *error) {
        NSMutableArray *array = [NSMutableArray arrayWithArray:_images];
        [array addObjectsFromArray:result];
        self.images = [array copy];
        [self.tableView.mj_footer endRefreshing];
        if ([(NSArray *)result count] < 15) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            //self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
    }];
}

- (void)setupUI {
    [self.tableView registerNib:[UINib nibWithNibName:@"HomePageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getProductList)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMore)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer.hidden = YES;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.images count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify];
    [cell setupCell:self.images[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScreenHeight;
}

@end
